//
//  KongWDogWebViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/24.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "KongWDogWebViewController.h"
#import "KongWDogWebDetailViewController.h"
#import "KongWSearchViewController.h"
#define kHeight     [UIScreen mainScreen].bounds.size.height / 667
#define kWidth      [UIScreen mainScreen].bounds.size.width / 375
#define WEBHTTPS    [NSString stringWithFormat:@"http://app.5ichong.com/web.php/tag/category?cid=%ld", self.taget]
@interface KongWDogWebViewController ()<UIWebViewDelegate>
@property (retain, nonatomic) UIWebView *web;

@property (copy, nonatomic) NSString *string;

@property (retain, nonatomic) NSArray *titleArray;
@end

@implementation KongWDogWebViewController
-(void)dealloc
{
    [_web release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSArray *dogArray = @[@"哈士奇", @"萨摩耶", @"泰迪", @"松狮", @"吉娃娃", @"金毛", @"博美", @"比熊犬"];
    NSArray *catArray = @[@"狸花猫", @"短毛猫", @"布偶猫", @"波斯猫", @"金吉拉", @"暹罗猫", @"折耳猫", @"安哥拉"];
    
    if (self.taget == 1) {
        self.title = @"宠物狗";
        self.titleArray = [NSArray arrayWithArray:dogArray];
    }else
    {
        self.title = @"宠物猫";
        self.titleArray = [NSArray arrayWithArray:catArray];
    }
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 30);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"KongWback"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.web.delegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:WEBHTTPS]];
    [self.view addSubview:self.web];
    [self.web loadRequest:request];
    [self.web release];
    
    [self creatBtn];
    
    
}
-(void)creatBtn
{
    for (int i = 0; i < 4; i ++) {
        for (int j = 0; j < 2; j ++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(self.web.frame.size.width / 4 * i, kHeight * 116 * j, self.web.frame.size.width / 4, kHeight * 116);
            btn.tag = 11000 + j * 4 + i;
            btn.alpha = 0.1;
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.web.scrollView addSubview:btn];
        }
    }


}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    self.string = [NSString stringWithFormat:@"%@", request.URL];
    if (![self.string isEqualToString:WEBHTTPS]){
        [((UILabel *)[self.web.scrollView viewWithTag:11111]) removeFromSuperview];
        KongWDogWebDetailViewController *dogWebDetail = [[KongWDogWebDetailViewController alloc] init];
        dogWebDetail.url = self.string;
        [self.navigationController pushViewController:dogWebDetail animated:YES];
        [dogWebDetail release];
        return NO;
    }
    
    return YES;
}

-(void)btnAction:(UIButton *)sender
{
    KongWSearchViewController *search = [[KongWSearchViewController alloc] init];
    search.keyWord = [self.titleArray objectAtIndex:sender.tag - 11000];
    [self.navigationController pushViewController:search animated:YES];
    [search release];
}
-(void)backBtnAction
{
        [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
